<?php

namespace App\Models;

use App\Handlers\KeysHandler;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

class Configuration extends Model
{

    public $timestamps = false;

    /**
     * 获取配置项
     * @param $key
     * @return mixed|null
     */
    public static function getItem($key)
    {
        $configurations = Cache::get(KeysHandler::CONFIGURATION);
        $configurations || $configurations = self::all();

        if ($configurations) {
            Cache::put(KeysHandler::CONFIGURATION, $configurations, KeysHandler::CONFIGURATION_EXPIRE);
            foreach ($configurations as $item) {
                if ($item->key === $key) {
                    return $item->value;
                }
            }
        }

        return null;
    }


    public static function getItemObject($key)
    {
        $configurations = Cache::get(KeysHandler::CONFIGURATION);
        $configurations || $configurations = self::all();
        return $configurations->where('key', '=', $key)->first();
    }
}
